package com.stylefeng.guns.modular.netcar.controller;

import cn.hutool.core.bean.BeanUtil;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.Tools.CodeMsg;
import com.stylefeng.guns.core.common.Tools.Result;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.validater.Insert;
import com.stylefeng.guns.core.validater.Update;
import com.stylefeng.guns.modular.netcar.model.DriverInf;
import com.stylefeng.guns.modular.netcar.service.IDriverInfService;
import com.stylefeng.guns.modular.netcar.warpper.DriverInfoWarpper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 驾驶员信息
 *
 * @author sasazaixian
 * @Date 2018-10-23 11:06
 */
@Controller
@RequestMapping(value = {"/gunsApi/auth/driverInf"})
@Api(value = "驾驶员信息操作相关api", tags = {"驾驶员信息操作接口"})
@Slf4j(topic = "驾驶员信息")
public class DriverInfController extends BaseController {
    @Inject
    private IDriverInfService driverInfService;
	
    /**
     * 分页获取驾驶员信息列表
     * @param driverInf
     * @param pageNum
     * @param pageSize
     * @return	
     */
    @ApiOperation(value = "分页获取驾驶员信息列表", notes = "分页获取驾驶员信息列表", httpMethod = "POST")
    @PostMapping(value = "/pageList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result pageList(
        @RequestBody @ApiParam(name = "driverInf", value = "驾驶员信息对象")DriverInf driverInf,
        @ApiParam(name = "pageNum", value = "当前页数", required = true) @RequestParam(value = "pageNum", defaultValue = "1",required = true) Integer pageNum,
        @ApiParam(name = "pageSize", value = "每页显示条数", required = true) @RequestParam(value = "pageSize", defaultValue = "1000000",required = true) Integer pageSize
        ) {
        try {
            Pager pager=new Pager();
            pager.setPageNumber(pageNum);//页码从1 开始
            pager.setPageSize(pageSize);
            pager.setObj(driverInf);
            Map map=driverInfService.pageListDriverInf(pager,getToken());
      if (map.size() > 0) {
        List<DriverInf> list = (List<DriverInf>) map.get("rows");
        if (list != null && !list.isEmpty()) {
            List<Map<String, Object>> mList=new ArrayList<>();
            for (DriverInf o:list){
                Map map1= BeanUtil.beanToMap(o);
                mList.add(map1);
            }
            super.warpObject(new DriverInfoWarpper(mList));
            BeanUtil.setFieldValue(map,"rows",mList);
        }
        }
        return Result.success(map);
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 获取驾驶员信息列表
     * @param driverInf    
     * @return	
     */
    @ApiOperation(value = "获取驾驶员信息列表", notes = "获取驾驶员信息列表", httpMethod = "POST")
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)	
    @ResponseBody
    public Result list(
        @RequestBody @ApiParam(name = "driverInf", value = "驾驶员信息对象")DriverInf driverInf) {
        try {            
            return Result.success( driverInfService.selectListDriverInf(driverInf,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 通过主键获取驾驶员信息对象
     * @param id
     * @return
     */
    @ApiOperation(value = "通过主键获取驾驶员信息对象", notes = "通过主键获取驾驶员信息对象", httpMethod = "GET")
    @GetMapping(value = "/getById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result getById(
            @ApiParam(name = "id", value = "主键ID", required = true) @RequestParam(value = "id", required = true) String  id
    ) {
        try {
            DriverInf driverInf=driverInfService.findDriverInfById(id,getToken());
            List<Map<String, Object>> mList=new ArrayList<>();
            if (driverInf!=null){
                    Map map1= BeanUtil.beanToMap(driverInf);
                    mList.add(map1);
             }
            return Result.success(super.warpObject(new DriverInfoWarpper(mList)));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }

    /**
     * 新增驾驶员信息数据
     *
     * @param driverInf 
     * @param result
     * @return
     */
    @ApiOperation(value = "新增驾驶员信息数据", notes = "新增驾驶员信息数据", httpMethod = "POST")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "新增驾驶员信息数据")
    @ResponseBody
    public Result add(@Validated(value = Insert.class) @RequestBody @ApiParam(name = "driverInf ", value = "驾驶员信息对象")DriverInf driverInf ,BindingResult result
    ) {
        if (result.hasErrors()) {
            return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
        }
        try {
            String tel=driverInf.getDiPhone();
            String salt= ShiroKit.getRandomSalt(5);
            driverInf.setDiSalt(salt);
            driverInf.setDiPassword(ShiroKit.md5(StringUtils.substring(StringUtils.trimToNull(tel),-6), salt));
            driverInf.setDiCheckResult("1");
            driverInfService.insertDriverInf(driverInf,getToken());
            
            return Result.success();
        }catch(Exception e){
            String msg=e.getMessage();
            if(msg.indexOf("for key 'index_2'")>-1 || msg.indexOf("for key 'index_3'")>-1){
                msg="手机号码或者证件号码已经存在！";
            }
            return Result.error(CodeMsg.SERVER_EXCEPTION,msg);
        }
    }


    /**
     * 更新驾驶员信息数据
     * @param driverInf
     * @param result
     * @return
     */
    @ApiOperation(value = "更新驾驶员信息数据", notes = "更新驾驶员信息数据", httpMethod = "POST")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "更新驾驶员信息数据")
    @ResponseBody
    public Result update(@Validated(value = Update.class) @RequestBody @ApiParam(name = "driverInf", value = "驾驶员信息对象")DriverInf driverInf, BindingResult result
    ) {
        if (result.hasErrors()) {
            return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
        }
        try {
            driverInfService.updateDriverInf(driverInf,getToken());
            return Result.success();
        }catch(Exception e){
            String msg=e.getMessage();
            if(msg.indexOf("for key 'index_2'")>-1 || msg.indexOf("for key 'index_3'")>-1){
                msg="手机号码或者证件号码已经存在！";
            }
            return Result.error(CodeMsg.SERVER_EXCEPTION,msg);
        }
    }

    /**
     * 重置驾驶员密码
     * @param id
     * @return
     */
    @ApiOperation(value = "更新驾驶员信息数据", notes = "更新驾驶员信息数据", httpMethod = "POST")
    @PostMapping(value = "/updatePws")
    @BussinessLog(value = "更新驾驶员信息数据")
    @ResponseBody
    public Result updatePws(@ApiParam(name = "id", value = "主键ID", required = true) @RequestParam(value = "id", required = true) String  id
    ) {
        try {
            driverInfService.updatePsw(id,getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 删除驾驶员信息数据
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除驾驶员信息数据", notes = "删除驾驶员信息数据", httpMethod = "GET")
    @GetMapping(value = "/delBatch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "删除驾驶员信息数据")
    @ResponseBody
    public Result delBatch(
            @ApiParam(name = "ids", value = "主键ID字符数组", required = true) @RequestParam(value = "ids", required = true) String ids
    ) {
        try {
            driverInfService.deleteBatchDriverInf(ids.split(","),getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
	/**
     * 删除驾驶员信息数据
     * @param driverInf
     * @return
     */
    @ApiOperation(value = "删除驾驶员信息数据", notes = "删除驾驶员信息数据", httpMethod = "POST")
    @PostMapping(value = "/del", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "删除驾驶员信息数据")
    @ResponseBody
    public Result del(@RequestBody @ApiParam(name = "driverInf", value = "驾驶员信息对象")DriverInf driverInf) {
        try {
            driverInfService.deleteDriverInf(driverInf,getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 获取驾驶员信息的Key-Value列表，用于html的Select数据源
     * @param driverInf
     * @return
     */
    @ApiOperation(value = "获取驾驶员信息的Key-Value列表，用于html的Select数据源", notes = "获取驾驶员信息的Key-Value列表，用于html的Select数据源", httpMethod = "POST")
    @PostMapping(value = "/optionsList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result optionsList(@RequestBody @ApiParam(name = "driverInf", value = "驾驶员信息对象")DriverInf driverInf) {
        try {
            return Result.success(driverInfService.optionsDriverInf(driverInf,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
	private String getToken(){		
		return getHttpServletRequest().getHeader("Authorization").substring(7);
    }
}
